home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / gawk-3.000 / gawk-3 / gawk-3.0.0 / awk.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-29  |  23.2 KB  |  823 lines

  1. /*
  2.  * awk.h -- Definitions for gawk. 
  3.  */
  4.  
  5. /* 
  6.  * Copyright (C) 1986, 1988, 1989, 1991-1995 the Free Software Foundation, Inc.
  7.  * 
  8.  * This file is part of GAWK, the GNU implementation of the
  9.  * AWK Programming Language.
  10.  * 
  11.  * GAWK is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  * 
  16.  * GAWK is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  * 
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
  24.  */
  25.  
  26. /* ------------------------------ Includes ------------------------------ */
  27.  
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31.  
  32. #include <stdio.h>
  33. #ifdef HAVE_LIMITS_H
  34. #include <limits.h>
  35. #endif /* HAVE_LIMITS_H */
  36. #include <ctype.h>
  37. #include <setjmp.h>
  38. #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
  39. #include <stdarg.h>
  40. #else
  41. #include <varargs.h>
  42. #endif
  43. #include <signal.h>
  44. #include <time.h>
  45. #include <errno.h>
  46. #if ! defined(errno) && ! defined(MSDOS) && ! defined(OS2)
  47. extern int errno;
  48. #endif
  49. #ifdef HAVE_SIGNUM_H
  50. #include <signum.h>
  51. #endif
  52.  
  53. /* ----------------- System dependencies (with more includes) -----------*/
  54.  
  55. /* This section is the messiest one in the file, not a lot that can be done */
  56.  
  57. #ifdef __STDC__
  58. #define    P(s)    s
  59. #define MALLOC_ARG_T size_t
  60. #else    /* not __STDC__ */
  61. #define    P(s)    ()
  62. #define MALLOC_ARG_T unsigned
  63. #define volatile
  64. #define const
  65. #endif    /* not __STDC__ */
  66.  
  67. #if ! defined(VMS) || (! defined(VAXC) && ! defined(__DECC))
  68. #include <sys/types.h>
  69. #include <sys/stat.h>
  70. #else    /* VMS w/ VAXC or DECC */
  71. #include <types.h>
  72. #include <stat.h>
  73. #include <file.h>    /* avoid <fcntl.h> in io.c */
  74. #ifdef __DECC
  75. /* DEC C implies DECC$SHR, which doesn't have the %g problem of VAXCRTL */
  76. #undef GFMT_WORKAROUND
  77. #endif
  78. #endif    /* VMS w/ VAXC or DECC */
  79.  
  80. #ifdef STDC_HEADERS
  81. #include <stdlib.h>
  82. #else    /* not STDC_HEADERS */
  83. #include "protos.h"
  84. #endif    /* not STDC_HEADERS */
  85.  
  86. #ifdef HAVE_STRING_H
  87. #include <string.h>
  88. #ifdef NEED_MEMORY_H
  89. #include <memory.h>
  90. #endif    /* NEED_MEMORY_H */
  91. #else    /* not HAVE_STRING_H */
  92. #ifdef HAVE_STRINGS_H
  93. #include <strings.h>
  94. #endif    /* HAVE_STRINGS_H */
  95. #endif    /* not HAVE_STRING_H */
  96.  
  97. #ifdef NeXT
  98. #if __GNUC__ < 2 || __GNUC_MINOR__ < 7
  99. #include <libc.h>
  100. #endif
  101. #undef atof
  102. #define getopt GNU_getopt
  103. #define GFMT_WORKAROUND
  104. #endif    /* NeXT */
  105.  
  106. #if defined(atarist) || defined(VMS)
  107. #include <unixlib.h>
  108. #endif    /* atarist || VMS */
  109.  
  110. #if HAVE_UNISTD_H
  111. #include <unistd.h>
  112. #endif    /* HAVE_UNISTD_H */
  113.  
  114. #ifndef HAVE_VPRINTF
  115. /* if you don't have vprintf, try this and cross your fingers. */
  116. #ifdef    HAVE_DOPRNT
  117. #define vfprintf(fp,fmt,arg)    _doprnt((fmt), (arg), (fp))
  118. #else    /* not HAVE_DOPRNT */
  119. you
  120. lose
  121. #endif    /* not HAVE_DOPRNT */
  122. #endif    /* HAVE_VPRINTF */
  123.  
  124. #ifdef VMS
  125. #include "vms/redirect.h"
  126. #endif  /*VMS*/
  127.  
  128. #ifdef atarist
  129. #include "atari/redirect.h"
  130. #endif
  131.  
  132. #define    GNU_REGEX
  133. #ifdef GNU_REGEX
  134. #include "regex.h"
  135. #include "dfa.h"
  136. typedef struct Regexp {
  137.     struct re_pattern_buffer pat;
  138.     struct re_registers regs;
  139.     struct dfa dfareg;
  140.     int dfa;
  141. } Regexp;
  142. #define    RESTART(rp,s)    (rp)->regs.start[0]
  143. #define    REEND(rp,s)    (rp)->regs.end[0]
  144. #define    SUBPATSTART(rp,s,n)    (rp)->regs.start[n]
  145. #define    SUBPATEND(rp,s,n)    (rp)->regs.end[n]
  146. #endif    /* GNU_REGEX */
  147.  
  148. /* ------------------ Constants, Structures, Typedefs  ------------------ */
  149.  
  150. #ifndef AWKNUM
  151. #define AWKNUM    double
  152. #endif
  153.  
  154. #ifndef TRUE
  155. /* a bit hackneyed, but what the heck */
  156. #define TRUE    1
  157. #define FALSE    0
  158. #endif
  159.  
  160. /* Figure out what '\a' really is. */
  161. #ifdef __STDC__
  162. #define BELL    '\a'        /* sure makes life easy, don't it? */
  163. #else
  164. #    if 'z' - 'a' == 25    /* ascii */
  165. #        if 'a' != 97    /* machine is dumb enough to use mark parity */
  166. #            define BELL    '\207'
  167. #        else
  168. #            define BELL    '\07'
  169. #        endif
  170. #    else
  171. #        define BELL    '\057'
  172. #    endif
  173. #endif
  174.  
  175. typedef enum nodevals {
  176.     /* illegal entry == 0 */
  177.     Node_illegal,
  178.  
  179.     /* binary operators  lnode and rnode are the expressions to work on */
  180.     Node_times,
  181.     Node_quotient,
  182.     Node_mod,
  183.     Node_plus,
  184.     Node_minus,
  185.     Node_cond_pair,        /* conditional pair (see Node_line_range) */
  186.     Node_subscript,
  187.     Node_concat,
  188.     Node_exp,
  189.  
  190.     /* unary operators   subnode is the expression to work on */
  191.     Node_preincrement,
  192.     Node_predecrement,
  193.     Node_postincrement,
  194.     Node_postdecrement,
  195.     Node_unary_minus,
  196.     Node_field_spec,
  197.  
  198.     /* assignments   lnode is the var to assign to, rnode is the exp */
  199.     Node_assign,
  200.     Node_assign_times,
  201.     Node_assign_quotient,
  202.     Node_assign_mod,
  203.     Node_assign_plus,
  204.     Node_assign_minus,
  205.     Node_assign_exp,
  206.  
  207.     /* boolean binaries   lnode and rnode are expressions */
  208.     Node_and,
  209.     Node_or,
  210.  
  211.     /* binary relationals   compares lnode and rnode */
  212.     Node_equal,
  213.     Node_notequal,
  214.     Node_less,
  215.     Node_greater,
  216.     Node_leq,
  217.     Node_geq,
  218.     Node_match,
  219.     Node_nomatch,
  220.  
  221.     /* unary relationals   works on subnode */
  222.     Node_not,
  223.  
  224.     /* program structures */
  225.     Node_rule_list,        /* lnode is a rule, rnode is rest of list */
  226.     Node_rule_node,        /* lnode is pattern, rnode is statement */
  227.     Node_statement_list,    /* lnode is statement, rnode is more list */
  228.     Node_if_branches,    /* lnode is to run on true, rnode on false */
  229.     Node_expression_list,    /* lnode is an exp, rnode is more list */
  230.     Node_param_list,    /* lnode is a variable, rnode is more list */
  231.  
  232.     /* keywords */
  233.     Node_K_if,        /* lnode is conditonal, rnode is if_branches */
  234.     Node_K_while,        /* lnode is condtional, rnode is stuff to run */
  235.     Node_K_for,        /* lnode is for_struct, rnode is stuff to run */
  236.     Node_K_arrayfor,    /* lnode is for_struct, rnode is stuff to run */
  237.     Node_K_break,        /* no subs */
  238.     Node_K_continue,    /* no subs */
  239.     Node_K_print,        /* lnode is exp_list, rnode is redirect */
  240.     Node_K_printf,        /* lnode is exp_list, rnode is redirect */
  241.     Node_K_next,        /* no subs */
  242.     Node_K_exit,        /* subnode is return value, or NULL */
  243.     Node_K_do,        /* lnode is conditional, rnode stuff to run */
  244.     Node_K_return,        /* lnode is return value */
  245.     Node_K_delete,        /* lnode is array, rnode is subscript */
  246.     Node_K_getline,        /* lnode is opt var, rnode is redirection */
  247.     Node_K_function,    /* lnode is statement list, rnode is params */
  248.     Node_K_nextfile,    /* no subs */
  249.  
  250.     /* I/O redirection for print statements */
  251.     Node_redirect_output,    /* subnode is where to redirect */
  252.     Node_redirect_append,    /* subnode is where to redirect */
  253.     Node_redirect_pipe,    /* subnode is where to redirect */
  254.     Node_redirect_pipein,    /* subnode is where to redirect */
  255.     Node_redirect_input,    /* subnode is where to redirect */
  256.  
  257.     /* Variables */
  258.     Node_var,        /* rnode is value, lnode is array stuff */
  259.     Node_var_array,        /* array is ptr to elements, asize num of eles */
  260.     Node_val,        /* node is a value - type in flags */
  261.  
  262.     /* Builtins   subnode is explist to work on, proc is func to call */
  263.     Node_builtin,
  264.  
  265.     /*
  266.      * pattern: conditional ',' conditional ;  lnode of Node_line_range
  267.      * is the two conditionals (Node_cond_pair), other word (rnode place)
  268.      * is a flag indicating whether or not this range has been entered.
  269.      */
  270.     Node_line_range,
  271.  
  272.     /*
  273.      * boolean test of membership in array lnode is string-valued
  274.      * expression rnode is array name 
  275.      */
  276.     Node_in_array,
  277.  
  278.     Node_func,        /* lnode is param. list, rnode is body */
  279.     Node_func_call,        /* lnode is name, rnode is argument list */
  280.  
  281.     Node_cond_exp,        /* lnode is conditonal, rnode is if_branches */
  282.     Node_regex,        /* a regexp, text, compiled, flags, etc */
  283.     Node_hashnode,        /* an identifier in the symbol table */
  284.     Node_ahash,        /* an array element */
  285.     Node_NF,        /* variables recognized in the grammar */
  286.     Node_NR,
  287.     Node_FNR,
  288.     Node_FS,
  289.     Node_RS,
  290.     Node_FIELDWIDTHS,
  291.     Node_IGNORECASE,
  292.     Node_OFS,
  293.     Node_ORS,
  294.     Node_OFMT,
  295.     Node_CONVFMT
  296. } NODETYPE;
  297.  
  298. /*
  299.  * NOTE - this struct is a rather kludgey -- it is packed to minimize
  300.  * space usage, at the expense of cleanliness.  Alter at own risk.
  301.  */
  302. typedef struct exp_node {
  303.     union {
  304.         struct {
  305.             union {
  306.                 struct exp_node *lptr;
  307.                 char *param_name;
  308.                 long ll;
  309.             } l;
  310.             union {
  311.                 struct exp_node *rptr;
  312.                 struct exp_node *(*pptr)();
  313.                 Regexp *preg;
  314.                 struct for_loop_header *hd;
  315.                 struct exp_node **av;
  316.                 int r_ent;    /* range entered */
  317.             } r;
  318.             union {
  319.                 char *name;
  320.                 struct exp_node *extra;
  321.                 long xl;
  322.             } x;
  323.             short number;
  324.             unsigned char reflags;
  325. #                define    CASE    1
  326. #                define    CONST    2
  327. #                define    FS_DFLT    4
  328.         } nodep;
  329.         struct {
  330.             AWKNUM fltnum;    /* this is here for optimal packing of
  331.                      * the structure on many machines
  332.                      */
  333.             char *sp;
  334.             size_t slen;
  335.             long sref;
  336.             int idx;
  337.         } val;
  338.         struct {
  339.             struct exp_node *next;
  340.             char *name;
  341.             size_t length;
  342.             struct exp_node *value;
  343.         } hash;
  344. #define    hnext    sub.hash.next
  345. #define    hname    sub.hash.name
  346. #define    hlength    sub.hash.length
  347. #define    hvalue    sub.hash.value
  348.         struct {
  349.             struct exp_node *next;
  350.             struct exp_node *name;
  351.             struct exp_node *value;
  352.         } ahash;
  353. #define    ahnext    sub.ahash.next
  354. #define    ahname    sub.ahash.name
  355. #define    ahvalue    sub.ahash.value
  356.     } sub;
  357.     NODETYPE type;
  358.     unsigned short flags;
  359. #        define    MALLOC    1    /* can be free'd */
  360. #        define    TEMP    2    /* should be free'd */
  361. #        define    PERM    4    /* can't be free'd */
  362. #        define    STRING    8    /* assigned as string */
  363. #        define    STR    16    /* string value is current */
  364. #        define    NUM    32    /* numeric value is current */
  365. #        define    NUMBER    64    /* assigned as number */
  366. #        define    MAYBE_NUM 128    /* user input: if NUMERIC then
  367.                          * a NUMBER */
  368. #        define    ARRAYMAXED 256    /* array is at max size */
  369. #        define    SCALAR     512    /* used as scalar, can't be array */
  370.     char *vname;    /* variable's name */
  371. } NODE;
  372.  
  373. #define lnode    sub.nodep.l.lptr
  374. #define nextp    sub.nodep.l.lptr
  375. #define rnode    sub.nodep.r.rptr
  376. #define source_file    sub.nodep.x.name
  377. #define    source_line    sub.nodep.number
  378. #define    param_cnt    sub.nodep.number
  379. #define param    sub.nodep.l.param_name
  380.  
  381. #define subnode    lnode
  382. #define proc    sub.nodep.r.pptr
  383.  
  384. #define re_reg    sub.nodep.r.preg
  385. #define re_flags sub.nodep.reflags
  386. #define re_text lnode
  387. #define re_exp    sub.nodep.x.extra
  388. #define    re_cnt    sub.nodep.number
  389.  
  390. #define forsub    lnode
  391. #define forloop    rnode->sub.nodep.r.hd
  392.  
  393. #define stptr    sub.val.sp
  394. #define stlen    sub.val.slen
  395. #define stref    sub.val.sref
  396. #define    stfmt    sub.val.idx
  397.  
  398. #define numbr    sub.val.fltnum
  399.  
  400. #define var_value lnode
  401. #define var_array sub.nodep.r.av
  402. #define array_size sub.nodep.l.ll
  403. #define table_size sub.nodep.x.xl
  404.  
  405. #define condpair lnode
  406. #define triggered sub.nodep.r.r_ent
  407.  
  408. /* a regular for loop */
  409. typedef struct for_loop_header {
  410.     NODE *init;
  411.     NODE *cond;
  412.     NODE *incr;
  413. } FOR_LOOP_HEADER;
  414.  
  415. /* for "for(iggy in foo) {" */
  416. struct search {
  417.     NODE *sym;
  418.     size_t idx;
  419.     NODE *bucket;
  420.     NODE *retval;
  421. };
  422.  
  423. /* for faster input, bypass stdio */
  424. typedef struct iobuf {
  425.     const char *name;
  426.     int fd;
  427.     char *buf;
  428.     char *off;
  429.     char *end;
  430.     size_t size;    /* this will be determined by an fstat() call */
  431.     int cnt;
  432.     long secsiz;
  433.     int flag;
  434. #        define    IOP_IS_TTY    1
  435. #        define    IOP_IS_INTERNAL    2
  436. #        define    IOP_NO_FREE    4
  437. } IOBUF;
  438.  
  439. typedef void (*Func_ptr)();
  440.  
  441. /* structure used to dynamically maintain a linked-list of open files/pipes */
  442. struct redirect {
  443.     unsigned int flag;
  444. #        define    RED_FILE    1
  445. #        define    RED_PIPE    2
  446. #        define    RED_READ    4
  447. #        define    RED_WRITE    8
  448. #        define    RED_APPEND    16
  449. #        define    RED_NOBUF    32
  450. #        define    RED_USED    64    /* closed temporarily to reuse fd */
  451. #        define    RED_EOF        128
  452.     char *value;
  453.     FILE *fp;
  454.     FILE *ifp;    /* input fp, needed for PIPES_SIMULATED */
  455.     IOBUF *iop;
  456.     int pid;
  457.     int status;
  458.     struct redirect *prev;
  459.     struct redirect *next;
  460. };
  461.  
  462. /* structure for our source, either a command line string or a source file */
  463. struct src {
  464.        enum srctype { CMDLINE = 1, SOURCEFILE } stype;
  465.        char *val;
  466. };
  467.  
  468. /* longjmp return codes, must be nonzero */
  469. /* Continue means either for loop/while continue, or next input record */
  470. #define TAG_CONTINUE 1
  471. /* Break means either for/while break, or stop reading input */
  472. #define TAG_BREAK 2
  473. /* Return means return from a function call; leave value in ret_node */
  474. #define    TAG_RETURN 3
  475.  
  476. #ifndef INT_MAX
  477. #define INT_MAX ((int)(~(1 << (sizeof (int) * 8 - 1))))
  478. #endif
  479. #ifndef LONG_MAX
  480. #define LONG_MAX ((long)(~(1L << (sizeof (long) * 8 - 1))))
  481. #endif
  482. #ifndef ULONG_MAX
  483. #define ULONG_MAX (~(unsigned long)0)
  484. #endif
  485. #ifndef LONG_MIN
  486. #define LONG_MIN ((long)(-LONG_MAX - 1L))
  487. #endif
  488. #define HUGE    LONG_MAX 
  489.  
  490. /* -------------------------- External variables -------------------------- */
  491. /* gawk builtin variables */
  492. extern long NF;
  493. extern long NR;
  494. extern long FNR;
  495. extern int IGNORECASE;
  496. extern int RS_is_null;
  497. extern char *OFS;
  498. extern int OFSlen;
  499. extern char *ORS;
  500. extern int ORSlen;
  501. extern char *OFMT;
  502. extern char *CONVFMT;
  503. extern int CONVFMTidx;
  504. extern int OFMTidx;
  505. extern NODE *CONVFMT_node, *FIELDWIDTHS_node, *FILENAME_node;
  506. extern NODE *FNR_node, *FS_node, *IGNORECASE_node, *NF_node;
  507. extern NODE *NR_node, *OFMT_node, *OFS_node, *ORS_node, *RLENGTH_node;
  508. extern NODE *RSTART_node, *RS_node, *RT_node, *SUBSEP_node; 
  509. extern NODE **stack_ptr;
  510. extern NODE *Nnull_string;
  511. extern NODE **fields_arr;
  512. extern int sourceline;
  513. extern char *source;
  514. extern NODE *expression_value;
  515.  
  516. #if __GNUC__ < 2
  517. extern NODE *_t;    /* used as temporary in tree_eval */
  518. #endif
  519.  
  520. extern NODE *nextfree;
  521. extern int field0_valid;
  522. extern int do_traditional;
  523. extern int do_posix;
  524. extern int do_lint;
  525. extern int do_lint_old;
  526. extern int do_intervals;
  527. extern int in_begin_rule;
  528. extern int in_end_rule;
  529.  
  530. extern const char *myname;
  531.  
  532. extern char quote;
  533. extern char *defpath;
  534. extern char envsep;
  535.  
  536. extern char casetable[];    /* for case-independent regexp matching */
  537.  
  538. /* ------------------------- Pseudo-functions ------------------------- */
  539.  
  540. #define is_identchar(c) (isalnum(c) || (c) == '_')
  541.  
  542. #ifdef MPROF
  543. #define    getnode(n)    emalloc(n, NODE *, sizeof(NODE), "getnode")
  544. #ifndef DEBUG
  545. #define    freenode(n)    free(n)
  546. #endif
  547. #else    /* not MPROF */
  548. #define    getnode(n)    if (nextfree) n = nextfree, nextfree = nextfree->nextp;\
  549.             else n = more_nodes()
  550. #ifndef DEBUG
  551. #define    freenode(n)    ((n)->flags &= ~SCALAR, (n)->nextp = nextfree, nextfree = (n))
  552. #endif
  553. #endif    /* not MPROF */
  554.  
  555. #ifdef DEBUG
  556. #define    tree_eval(t)    r_tree_eval(t, FALSE)
  557. #define    m_tree_eval(t, iscond)    r_tree_eval(t, iscond)
  558. #define    get_lhs(p, a)    r_get_lhs((p), (a))
  559. #undef freenode
  560. #else
  561. #define    get_lhs(p, a)    ((p)->type == Node_var ? (&(p)->var_value) : \
  562.             r_get_lhs((p), (a)))
  563. #define tree_eval(t)    m_tree_eval(t, FALSE)
  564. #if __GNUC__ >= 2
  565. #define    m_tree_eval(t, iscond) \
  566.                         ({NODE * _t = (t);                 \
  567.                if (_t == NULL)                 \
  568.                    _t = Nnull_string;          \
  569.                else {                          \
  570.                    switch(_t->type) {          \
  571.                    case Node_val:              \
  572.                    break;                  \
  573.                    case Node_var:              \
  574.                    _t = _t->var_value;     \
  575.                    break;                  \
  576.                    default:                    \
  577.                    _t = r_tree_eval(_t, iscond);\
  578.                    break;                  \
  579.                    }                           \
  580.                }                               \
  581.                _t;})
  582. #else
  583. #define    m_tree_eval(t, iscond)    (_t = (t), _t == NULL ? Nnull_string : \
  584.             (_t->type == Node_param_list ? \
  585.               r_tree_eval(_t, iscond) : \
  586.             (_t->type == Node_val ? _t : \
  587.             (_t->type == Node_var ? _t->var_value : \
  588.               r_tree_eval(_t, iscond)))))
  589. #endif /* __GNUC__ */
  590. #endif /* not DEBUG */
  591.  
  592. #define    make_number(x)    mk_number((x), (unsigned int)(MALLOC|NUM|NUMBER))
  593. #define    tmp_number(x)    mk_number((x), (unsigned int)(MALLOC|TEMP|NUM|NUMBER))
  594.  
  595. #define    free_temp(n)    do { if ((n)->flags&TEMP) { unref(n); }} while (FALSE)
  596. #define    make_string(s, l)    make_str_node((s), (size_t) (l), FALSE)
  597. #define        SCAN            1
  598. #define        ALREADY_MALLOCED    2
  599.  
  600. #define    cant_happen()    fatal("internal error line %d, file: %s", \
  601.                 __LINE__, __FILE__);
  602.  
  603. #ifdef HAVE_STRINGIZE
  604. #define    emalloc(var,ty,x,str)    (void)((var=(ty)malloc((MALLOC_ARG_T)(x))) ||\
  605.                  (fatal("%s: %s: can't allocate memory (%s)",\
  606.                     (str), #var, strerror(errno)),0))
  607. #define    erealloc(var,ty,x,str)    (void)((var=(ty)realloc((char *)var,\
  608.                           (MALLOC_ARG_T)(x))) ||\
  609.                  (fatal("%s: %s: can't allocate memory (%s)",\
  610.                     (str), #var, strerror(errno)),0))
  611. #else /* HAVE_STRINGIZE */
  612. #define    emalloc(var,ty,x,str)    (void)((var=(ty)malloc((MALLOC_ARG_T)(x))) ||\
  613.                  (fatal("%s: %s: can't allocate memory (%s)",\
  614.                     (str), "var", strerror(errno)),0))
  615. #define    erealloc(var,ty,x,str)    (void)((var=(ty)realloc((char *)var,\
  616.                           (MALLOC_ARG_T)(x))) ||\
  617.                  (fatal("%s: %s: can't allocate memory (%s)",\
  618.                     (str), "var", strerror(errno)),0))
  619. #endif /* HAVE_STRINGIZE */
  620.  
  621. #ifdef DEBUG
  622. #define    force_number    r_force_number
  623. #define    force_string    r_force_string
  624. #else /* not DEBUG */
  625. #ifdef lint
  626. extern AWKNUM force_number();
  627. #endif
  628. #if __GNUC__ >= 2
  629. #define    force_number(n)    ({NODE *_tn = (n);\
  630.             (_tn->flags & NUM) ?_tn->numbr : r_force_number(_tn);})
  631. #define    force_string(s)    ({NODE *_ts = (s);\
  632.               ((_ts->flags & STR) && \
  633.                (_ts->stfmt == -1 || _ts->stfmt == CONVFMTidx)) ?\
  634.               _ts : r_force_string(_ts);})
  635. #else
  636. #ifdef MSDOS
  637. extern double _msc51bug;
  638. #define    force_number(n)    (_msc51bug=(_t = (n),\
  639.               (_t->flags & NUM) ? _t->numbr : r_force_number(_t)))
  640. #else /* not MSDOS */
  641. #define    force_number(n)    (_t = (n),\
  642.              (_t->flags & NUM) ? _t->numbr : r_force_number(_t))
  643. #endif /* not MSDOS */
  644. #define    force_string(s)    (_t = (s),((_t->flags & STR) && \
  645.                    (_t->stfmt == -1 || \
  646.                     _t->stfmt == CONVFMTidx))? \
  647.              _t : r_force_string(_t))
  648. #endif /* not __GNUC__ */
  649. #endif /* not DEBUG */
  650.  
  651. #define    STREQ(a,b)    (*(a) == *(b) && strcmp((a), (b)) == 0)
  652. #define    STREQN(a,b,n)    ((n) && *(a)== *(b) && \
  653.              strncmp((a), (b), (size_t) (n)) == 0)
  654.  
  655. /* ------------- Function prototypes or defs (as appropriate) ------------- */
  656.  
  657. /* array.c */
  658. extern NODE *concat_exp P((NODE *tree));
  659. extern void assoc_clear P((NODE *symbol));
  660. extern unsigned int hash P((const char *s, size_t len, unsigned long hsize));
  661. extern int in_array P((NODE *symbol, NODE *subs));
  662. extern NODE **assoc_lookup P((NODE *symbol, NODE *subs));
  663. extern void do_delete P((NODE *symbol, NODE *tree));
  664. extern void assoc_scan P((NODE *symbol, struct search *lookat));
  665. extern void assoc_next P((struct search *lookat));
  666. /* awktab.c */
  667. extern char *tokexpand P((void));
  668. extern char nextc P((void));
  669. extern NODE *node P((NODE *left, NODETYPE op, NODE *right));
  670. extern NODE *install P((char *name, NODE *value));
  671. extern NODE *lookup P((const char *name));
  672. extern NODE *variable P((char *name, int can_free, NODETYPE type));
  673. extern int yyparse P((void));
  674. /* builtin.c */
  675. extern double double_to_int P((double d));
  676. extern NODE *do_exp P((NODE *tree));
  677. extern NODE *do_fflush P((NODE *tree));
  678. extern NODE *do_index P((NODE *tree));
  679. extern NODE *do_int P((NODE *tree));
  680. extern NODE *do_length P((NODE *tree));
  681. extern NODE *do_log P((NODE *tree));
  682. extern NODE *do_sprintf P((NODE *tree));
  683. extern void do_printf P((NODE *tree));
  684. extern void print_simple P((NODE *tree, FILE *fp));
  685. extern NODE *do_sqrt P((NODE *tree));
  686. extern NODE *do_substr P((NODE *tree));
  687. extern NODE *do_strftime P((NODE *tree));
  688. extern NODE *do_systime P((NODE *tree));
  689. extern NODE *do_system P((NODE *tree));
  690. extern void do_print P((NODE *tree));
  691. extern NODE *do_tolower P((NODE *tree));
  692. extern NODE *do_toupper P((NODE *tree));
  693. extern NODE *do_atan2 P((NODE *tree));
  694. extern NODE *do_sin P((NODE *tree));
  695. extern NODE *do_cos P((NODE *tree));
  696. extern NODE *do_rand P((NODE *tree));
  697. extern NODE *do_srand P((NODE *tree));
  698. extern NODE *do_match P((NODE *tree));
  699. extern NODE *do_gsub P((NODE *tree));
  700. extern NODE *do_sub P((NODE *tree));
  701. extern NODE *do_gensub P((NODE *tree));
  702. /* eval.c */
  703. extern int interpret P((NODE *volatile tree));
  704. extern NODE *r_tree_eval P((NODE *tree, int iscond));
  705. extern int cmp_nodes P((NODE *t1, NODE *t2));
  706. extern NODE **r_get_lhs P((NODE *ptr, Func_ptr *assign));
  707. extern void set_IGNORECASE P((void));
  708. void set_OFS P((void));
  709. void set_ORS P((void));
  710. void set_OFMT P((void));
  711. void set_CONVFMT P((void));
  712. /* field.c */
  713. extern void init_fields P((void));
  714. extern void set_record P((char *buf, int cnt, int freeold));
  715. extern void reset_record P((void));
  716. extern void set_NF P((void));
  717. extern NODE **get_field P((long num, Func_ptr *assign));
  718. extern NODE *do_split P((NODE *tree));
  719. extern void set_FS P((void));
  720. extern void set_FS_if_not_FIELDWIDTHS P((void));
  721. extern void set_RS P((void));
  722. extern void set_FIELDWIDTHS P((void));
  723. extern int using_fieldwidths P((void));
  724. /* gawkmisc.c */
  725. extern char *gawk_name P((const char *filespec));
  726. extern void os_arg_fixup P((int *argcp, char ***argvp));
  727. extern int os_devopen P((const char *name, int flag));
  728. extern int optimal_bufsize P((int fd, struct stat *sbuf));
  729. extern int ispath P((const char *file));
  730. extern int isdirpunct P((int c));
  731. /* io.c */
  732. extern void set_FNR P((void));
  733. extern void set_NR P((void));
  734. extern void do_input P((void));
  735. extern struct redirect *redirect P((NODE *tree, int *errflg));
  736. extern NODE *do_close P((NODE *tree));
  737. extern int flush_io P((void));
  738. extern int close_io P((void));
  739. extern int devopen P((const char *name, const char *mode));
  740. extern int pathopen P((const char *file));
  741. extern NODE *do_getline P((NODE *tree));
  742. extern void do_nextfile P((void));
  743. extern IOBUF *iop_alloc P((int fd, const char *name));
  744. /* main.c */
  745. extern int main P((int argc, char **argv));
  746. extern void load_environ P((void));
  747. extern char *arg_assign P((char *arg));
  748. extern RETSIGTYPE catchsig P((int sig, int code));
  749. /* msg.c */
  750. extern void err P((const char *s, const char *emsg, va_list argp));
  751. #if _MSC_VER == 510
  752. extern void msg P((va_list va_alist, ...));
  753. extern void error P((va_list va_alist, ...));
  754. extern void warning P((va_list va_alist, ...));
  755. extern void fatal P((va_list va_alist, ...));
  756. #else
  757. #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
  758. extern void msg (char *mesg, ...);
  759. extern void error (char *mesg, ...);
  760. extern void warning (char *mesg, ...);
  761. extern void fatal (char *mesg, ...);
  762. #else
  763. extern void msg ();
  764. extern void error ();
  765. extern void warning ();
  766. extern void fatal ();
  767. #endif
  768. #endif
  769. /* node.c */
  770. extern AWKNUM r_force_number P((NODE *n));
  771. extern NODE *r_force_string P((NODE *s));
  772. extern NODE *dupnode P((NODE *n));
  773. extern NODE *mk_number P((AWKNUM x, unsigned int flags));
  774. extern NODE *make_str_node P((char *s, size_t len, int scan ));
  775. extern NODE *tmp_string P((char *s, size_t len ));
  776. extern NODE *more_nodes P((void));
  777. #ifdef DEBUG
  778. extern void freenode P((NODE *it));
  779. #endif
  780. extern void unref P((NODE *tmp));
  781. extern int parse_escape P((char **string_ptr));
  782. /* re.c */
  783. extern Regexp *make_regexp P((char *s, size_t len, int ignorecase, int dfa));
  784. extern int research P((Regexp *rp, char *str, int start,
  785.                size_t len, int need_start));
  786. extern void refree P((Regexp *rp));
  787. extern void reg_error P((const char *s));
  788. extern Regexp *re_update P((NODE *t));
  789. extern void resyntax P((int syntax));
  790. extern void resetup P((void));
  791. extern int avoid_dfa P((NODE *re, char *str, size_t len));    /* temporary */
  792.  
  793. /* strncasecmp.c */
  794. extern int strncasecmp P((const char *s1, const char *s2, register size_t n));
  795.  
  796. #if defined(atarist)
  797. #if defined(PIPES_SIMULATED)
  798. /* atari/tmpnam.c */
  799. extern char *tmpnam P((char *buf));
  800. extern char *tempnam P((const char *path, const char *base));
  801. #else
  802. #include <wait.h>
  803. #endif
  804. #include <fcntl.h>
  805. #define INVALID_HANDLE  (__SMALLEST_VALID_HANDLE - 1)
  806. #else
  807. #define INVALID_HANDLE (-1)
  808. #endif /* atarist */
  809.  
  810. #ifndef STATIC
  811. #define STATIC static
  812. #endif
  813.  
  814. #ifdef C_ALLOCA
  815. /* The __hpux check is to avoid conflicts with bison's definition of
  816.    alloca() in awktab.c.*/
  817. #if (defined(__STDC__) && __STDC__) || defined (__hpux)
  818. extern void *alloca P((unsigned));
  819. #else
  820. extern char *alloca P((unsigned));
  821. #endif
  822. #endif
  823.